﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public struct Animation
    {
        private string name;
        private int startCol;
        private int endCol;
        private int startRow;
        private int endRow;
        private bool oscilate;

        public string Name { get { return name; } private set { name = value; } }
        public int StartCol { get { return startCol; } private set { startCol = value; } }
        public int EndCol { get { return endCol; } private set { endCol = value; } }
        public int StartRow { get { return startRow; } private set { startRow = value; } }
        public int EndRow { get { return endRow; } private set { endRow = value; } }
        public bool Oscilate { get { return oscilate; } private set { oscilate = value; } }

        public Animation(string Name, int StartCol, int EndCol, int StartRow, int EndRow, bool Oscilate)
        {
            this.name = Name;
            this.startCol = StartCol;
            this.startRow = StartRow;
            this.endCol = EndCol;
            this.endRow = EndRow;
            this.oscilate = Oscilate;
        }
    }

    public partial class Form1 : Form
    {
        private int Columns = 1;
        private int Rows = 1;
        private Dictionary<String, Animation> Animations;
        private Image CurrentSpriteSheet = null;

        public Form1()
        {
            InitializeComponent();
            Animations = new Dictionary<String, Animation>();
            AddDefaultAnimation(true);
        }

        private void AddDefaultAnimation(bool IgnoreRemoval)
        {
            if (Animations.Remove("default") || IgnoreRemoval)
            {
                Animation Default = new Animation("default",0,0,Columns-1,Rows-1,false);
                Animations.Add("default", Default);
            }
        }

        private void UpdateData()
        {
            if (CurrentSpriteSheet != null)
            {
                Bitmap b = new Bitmap(CurrentSpriteSheet);

                using (Graphics g = Graphics.FromImage(b))
                {
                    int Width = CurrentSpriteSheet.Width;
                    int Height = CurrentSpriteSheet.Height;


                    for (int i = 0; i < Rows; i++)
                    {
                        g.DrawLine(new Pen(Color.Red), new Point(0, i * (Height / Rows)), new Point(Width, i * (Height / Rows)));
                    }

                    for (int i = 0; i < Columns; i++)
                    {
                        g.DrawLine(new Pen(Color.Red), new Point(i * (Width / Columns), 0), new Point(i * (Width / Columns), Height));
                    }

                    g.DrawLine(new Pen(Color.Red), new Point(0, Height-1), new Point(Width-1, Height-1));
                    g.DrawLine(new Pen(Color.Red), new Point(Width-1, 0), new Point(Width-1, Height-1));

                }

                spriteSheetPic.Image = b;
            }
            else
            {
                spriteSheetPic.Image = null;
            }
        }

        private void imageLocTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                CurrentSpriteSheet = new Bitmap(imageLocTextBox.Text);
            }
            catch (Exception)
            {
                CurrentSpriteSheet = null;
            }

            UpdateData();
        }

        private void colTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Columns = Convert.ToInt32(colTextBox.Text);
            }
            catch (Exception)
            {
                Columns = 1;
            }

            UpdateData();
        }

        private void rowTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Rows = Convert.ToInt32(rowTextBox.Text);
            }
            catch (Exception)
            {
                Rows = 1;
            }

            UpdateData();
        }

        private void newAnimButton_Click(object sender, EventArgs e)
        {
            new AnimationEditor(this).Show();
        }

        private void EditAnimButton_Click(object sender, EventArgs e)
        {
            Animation temp;
            if (Animations.TryGetValue((String)animationsListBox.SelectedItem, out temp))
            {
                new AnimationEditor(temp, this).Show();
            }
            else
            {
                new AnimationEditor(this).Show();
            }
        }

        private void animationsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (animationsListBox.SelectedItem != null)
            {
                removeAnimButton.Enabled = true;
                EditAnimButton.Enabled = true;
            }
            else
            {
                removeAnimButton.Enabled = false;
                EditAnimButton.Enabled = false;
            }
        }

        public void AddReplaceAnimation(String editname, String name, Animation animation)
        {
            if(editname != null)
            {
                Animations.Remove(editname);
                animationsListBox.Items.Remove(editname);
            }
            Animations.Add(name, animation);
            animationsListBox.Items.Add(name);
        }

        private void removeAnimButton_Click(object sender, EventArgs e)
        {
            Animations.Remove((String)animationsListBox.SelectedItem);
            animationsListBox.Items.Remove(animationsListBox.SelectedItem);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (imageLocTextBox.Text == "")
            {
                MessageBox.Show("Please Load an Image File First", "No Image File",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (CurrentSpriteSheet == null)
            {
                MessageBox.Show("Image File Not Found", "No Image File",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {
                StreamWriter writer = new StreamWriter(saveLocTextBox.Text);
                
                writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                writer.WriteLine("<XnaContent>");
                writer.WriteLine("<Asset Type=\"SquogContentPipeline.SpriteSheetData\">");
                writer.WriteLine("");
                writer.WriteLine("<!-- Created using AssestEditor - Justin Fouts -->");
                writer.WriteLine("");

                writer.WriteLine("  <ImageLocation>" + imageLocTextBox.Text + "</ImageLocation>");
                writer.WriteLine("  <Columns>" + Columns + "</Columns>");
                writer.WriteLine("  <Rows>" + Rows + "</Rows>");
                if (scaleTextBox.Text == "")
                {
                    writer.WriteLine("  <Scale>1</Scale>");
                }
                else
                {
                    writer.WriteLine("  <Scale>" + scaleTextBox.Text + "</Scale>");
                }

                writer.WriteLine("  <Animations>");

                foreach (Animation a in Animations.Values)
                {
                    writer.WriteLine("    <Item>");
                    writer.WriteLine("      <Name>" + a.Name + "</Name>");
                    writer.WriteLine("      <StartColumn>" + a.StartCol + "</StartColumn>");
                    writer.WriteLine("      <EndColumn>" + a.EndCol + "</EndColumn>");
                    writer.WriteLine("      <StartRow>" + a.StartRow + "</StartRow>");
                    writer.WriteLine("      <EndRow>" + a.EndRow + "</EndRow>");
                    writer.WriteLine("      <Oscillate>" + a.Oscilate.ToString().ToLower() + "</Oscillate>");
                    writer.WriteLine("    </Item>");
                }

                writer.WriteLine("  </Animations>");
                writer.WriteLine("</Asset>");
                writer.WriteLine("</XnaContent>");
                writer.Close();

                MessageBox.Show("File Saved Successfuly", "Success",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception)
            {
                MessageBox.Show("Error writing to file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog browseFile = new OpenFileDialog();
            browseFile.Filter = "Image Files (*.png)|*.png";
            browseFile.Title = "Browse Image File";
            if (browseFile.ShowDialog() == DialogResult.Cancel)
                return;
            try
            {
                imageLocTextBox.Text = browseFile.FileName;
            }
            catch (Exception)
            {
                MessageBox.Show("Error opening file", "File Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
